package puzzle.game.states;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.BasicComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.state.StateBasedGame;
import puzzle.core.TileFactory;
import puzzle.game.BallPuzzle;
import puzzle.game.states.ExitState;
import puzzle.game.states.HelpState;
import puzzle.game.states.LoadingState;
import puzzle.game.states.PuzzleState;
import puzzle.game.states.StandardIngameState;
import puzzle.game.ui.PuzzleButton;
import puzzle.util.ResourceManager;

public class MenuState extends PuzzleState implements ComponentListener {
   
  public static final int ID = 2;
  
  public MenuState(BallPuzzle puz) {
    super(puz);
  }
  
  private final PuzzleButton[] buttons = new PuzzleButton[5];
  private Color upCol = new Color(163, 209, 242);
  private Color downCol = new Color(193, 228, 248);
  private int logox=0,logoy=0;
  private Image logo;
  private Color trans;
  private boolean fading = false;
  
  public void initState(GameContainer c) throws SlickException {
    setBackground(TileFactory.GROUND_COLOR);
    trans = new Color(getBackground());
    trans.a = 1f;
  }
  
  public void startState(GameContainer c) throws SlickException {
    int padding = 5;
    
    buttons[0] = createButton("game.msg.continue", "Continue");
    buttons[1] = createButton("game.msg.new", "New Game");
    buttons[2] = createButton("game.msg.user", "Play User Maps");
    buttons[3] = createButton("game.msg.help", "Help");
    buttons[4] = createButton("game.msg.quit", "Quit");
    
    int maxWidth = 0;
    int totalHeight = 0;
    for (int i=0; i<buttons.length; i++) {
      buttons[i].addComponentListener(this);
      maxWidth = Math.max(maxWidth, buttons[i].getWidth());
      totalHeight += buttons[i].getHeight();
    }
    
    int cx = c.getWidth()/2 - maxWidth/2;
    int cy = (c.getHeight()/2 - totalHeight/2)-buttons[0].getHeight()-padding;
    
    int curheight = 0;
    for (int i=0; i<buttons.length; i++) {
      curheight += buttons[i].getHeight();
      int nx = cx;
      int ny = cy+padding+curheight;
      buttons[i].setLocation(nx, ny);
    }
    
    logo = getContext().getLoadingState().getSmallLogo();
    logox = c.getWidth()/2-logo.getWidth()/2;
    logoy = buttons[0].getY() - padding - logo.getHeight();
    
    fading = true;
  }
  
  public void renderState(GameContainer c, Graphics g) {
    if (logo==null)
      return;
    
    g.drawImage(logo, logox, logoy);
    
    if (trans.a>0) {
      g.setColor(trans);
      g.fillRect(logox, logoy, logo.getWidth(), logo.getHeight());
    }
    
    try
	{
    for (int i=0; i<buttons.length; i++) {
      buttons[i].render(c, g);
    }
		getContext().getSoundSwitch().render(c, g);
	} catch (SlickException e)
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  
  private PuzzleButton createButton(String key, String def) {
    String text = ResourceManager.getString(key, def);
    
    GameContainer c = getContext().getContainer();
    Font f = getContext().getLoadingState().getMainFont();
    PuzzleButton b = new PuzzleButton(c, f, text);
    b.setColor(PuzzleButton.ROLLOVER, Color.white);
    b.setColor(PuzzleButton.UP, upCol);
    b.setColor(PuzzleButton.DOWN, downCol);
    
    return b;
  }
  
  public void componentActivated(AbstractComponent c) {
    //continue
    if (c==buttons[0]) {
      getContext().enterState(StandardIngameState.ID,
                        this.getExitTransition(),
                        this.getEnterTransition());  
    }
    //new game
    else if (c==buttons[1]) {
      getContext().getStandardIngameState().setMapIndex(0);
      getContext().enterState(StandardIngameState.ID,
                        this.getExitTransition(),
                        this.getEnterTransition());  
    }
    //user maps
    else if (c==buttons[2]) {
      getContext().enterState(UserIngameState.ID,
                        this.getExitTransition(),
                        this.getEnterTransition());
    }
    //help
    else if (c==buttons[3]) {
      getContext().enterState(HelpState.ID,
                        this.getExitTransition(),
                        this.getEnterTransition());
    }
    //quit
    else if (c==buttons[4]) {
      getContext().enterState(ExitState.ID,
                        this.getExitTransition(),
                        this.getEnterTransition());
    }
  }
  
  public void updateState(GameContainer c, int delta) {
    if (fading) {
        trans.a -= delta * 0.002f;
      if (trans.a <= 0) {
        trans.a = 0;
        fading = false;
      }
    }
  }
  
  public void keyPressed(int key, char c) {
    super.keyPressed(key, c);
    if (key == Input.KEY_ESCAPE)
    {
      getContext().enterState(ExitState.ID,this.getExitTransition(),this.getEnterTransition());
    } else if (key == Input.KEY_SPACE)
    {
    	
    }
  }
  
  public void enter(GameContainer container, StateBasedGame game) throws SlickException {
    super.enter(container, game);
    
    playTransitionSound();
  }
  
  public void leave(GameContainer container, StateBasedGame game) throws SlickException {
    super.leave(container, game);
    
    Sound trans = getContext().getLoadingState().getTransitionSound();
    if (trans!=null) {
        if (trans.playing())
            trans.stop();
    }
  }
  
  public void playTransitionSound() {
    Sound trans = getContext().getLoadingState().getTransitionSound();
    if (trans!=null) {
        if (!trans.playing()) {
            trans.play(1.0f, .1f);
        }
    }
  }
  
  public int getID() {
    return ID;
  }

}
